package com.example.hp.yuejian5.activity;


import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;

import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.RetrofitListener.RetrofitListener;
import com.example.hp.yuejian5.bean.RegisterBean;
import com.example.hp.yuejian5.model.MyModel;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import static android.content.Context.MODE_PRIVATE;

public class MySetActivity extends AppCompatActivity {
    private EditText username,password,tel,email;
    private ImageView back;
    private RadioGroup gender;
    private RadioButton radioButton1,radioButton2;
    private Button makeSure;
    private String u_id;
    private SharedPreferences sp;
    private String sexflag;
    private int ugender;
 private RetrofitListener<RegisterBean> listener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_set);
        initView();
        initEvent();

        sp = getSharedPreferences("user_id", MODE_PRIVATE);//默认操作模式
        u_id=sp.getString("userid",null);
        listener=new RetrofitListener<RegisterBean>() {


            @Override
            public void onSuccess(RegisterBean registerBean, int flag) {
                if (registerBean.getSuccess().equals("1")) {
                    Toast.makeText(MySetActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MySetActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(MySetActivity.this, "用户名已被占用", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFail() {
                Toast.makeText(MySetActivity.this, "修改失败", Toast.LENGTH_SHORT).show();

            }
        };


    }
    public void initView(){
        username=findViewById(R.id.mine_edit_user);
        password=findViewById(R.id.mine_edit_password);
        tel=findViewById(R.id.mine_edit_phone);
        gender=findViewById(R.id.mine_edit_sex);
        email=findViewById(R.id.mine_edit_email);

        radioButton1=findViewById(R.id.man);
        radioButton2=findViewById(R.id.woman);
        makeSure=findViewById(R.id.mine_edit_edit_bt);
        back=findViewById(R.id.mine_edit_back);
    }
private void initEvent(){
   makeSure.setOnClickListener(new View.OnClickListener(){
       public void onClick(View v){
           edit();
       }
    });
    radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked)
            {
                sexflag="1";
                radioButton1.setText("男");
            }
        }
    });

    radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked)
            {
                sexflag="0";
                radioButton1.setText("女");
            }
        }
    });

}
private void edit() {
    MyModel myModel = new MyModel();
    myModel.MineEdit(u_id,username.getText().toString().trim(),
            password.getText().toString().trim(),
            tel.getText().toString().trim(),
            sexflag,
            email.getText().toString().trim(),
            listener);
}


}
